﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BusinnessLogic;
using Model;
using System.Xml;
using System.IO;
using System.Xml.Xsl;

namespace scrumbench
{
    public partial class MyViewForm : Form
    {
        //data member made in order to user the constructor with userid
        private int userid;

        private List<Tasks> thisuserstasks;

        public List<Tasks> Thisuserstasks
        {
            get { return thisuserstasks; }
            set { thisuserstasks = value; }
        }
        
        public MyViewForm(int userid)
        {
            this.userid = userid;

            InitializeComponent();
        }




        private void MyViewForm_Load(object sender, EventArgs e)
        {
 
            
            //Load user data
            MyView myviewer = new MyView();
            User currentuser = myviewer.mijnuser(userid);

            lblName.Text = currentuser.name + ", " + currentuser.Firstname;
            lblRol.Text = currentuser.Rolnaam;
            
            List<string> usersteams = new List<string>();
            usersteams = myviewer.UserTeamRetriever(userid);
            lblTeam.Text = "";
            foreach(string str in usersteams)
            {
                lblTeam.Text = lblTeam.Text  + str+ "\r";
            }


            lblUID.Text = currentuser.UserID.ToString();
            lblUserName.Text = currentuser.login;


            //Load Tasklist
            List<Tasks> userstasks = new List<Tasks>();
            userstasks = myviewer.getmytasks(userid);

            foreach (Tasks atask in userstasks)
            {
                ListViewItem newitem = new ListViewItem();
                newitem.Text = atask.Titel;

                //ListViewItem.ListViewSubItem newsubitem = new ListViewItem.ListViewSubItem();
                //newsubitem.Text = atask.TaskID.ToString();
                //newitem.SubItems.Add(newsubitem);
                //ListViewItem.ListViewSubItem newsubitem1 = new ListViewItem.ListViewSubItem();
                //newsubitem1.Text = atask.Titel;
                //newitem.SubItems.Add(newsubitem1);
                ListViewItem.ListViewSubItem newsubitem2 = new ListViewItem.ListViewSubItem();
                newsubitem2.Text = atask.Status.ToString();
                newitem.SubItems.Add(newsubitem2);
                ListViewItem.ListViewSubItem newsubitem3 = new ListViewItem.ListViewSubItem();
                newsubitem3.Text = atask.Sprintduedate.ToString("yy-MM-dd");
                newitem.SubItems.Add(newsubitem3);
                ListViewItem.ListViewSubItem newsubitem4 = new ListViewItem.ListViewSubItem();
                newsubitem4.Text = atask.Sprinttitel.ToString();
                newitem.SubItems.Add(newsubitem4);
                ListViewItem.ListViewSubItem newsubitem5 = new ListViewItem.ListViewSubItem();
                newsubitem5.Text = atask.Teamname.ToString();
                newitem.SubItems.Add(newsubitem5);

                //adding the taskid in order to use when opening the taskdetail form
                
                
                newitem.Tag = atask.TaskID;

                this.LvAllTasks.Items.Add(newitem);

                //give a color to the status with a conditional filter
                if (newitem.SubItems[1].Text == "Not Started")
                {
                    newitem.BackColor = System.Drawing.Color.Red;
                }
                else if (newitem.SubItems[1].Text == "Started")
                {
                    newitem.BackColor = System.Drawing.Color.Orange;
                }
                else if (newitem.SubItems[1].Text == "Done")
                {
                    newitem.BackColor = System.Drawing.Color.Green;
                }

            }


            this.LvAllTasks.Sort();
            this.Thisuserstasks = userstasks;

                //Check all filterboxes ON
            this.CheckBoxShowOptions.SetItemChecked(0, true);
            this.CheckBoxShowOptions.SetItemChecked(1, true);
            this.CheckBoxShowOptions.SetItemChecked(2, true);

            #region Load Hot Tasks window
            //Load Hot Tasks (this is same list, but only for the coming 3 days and 
            foreach (Tasks atask in userstasks)
            {
                if (int.Parse(atask.Sprintduedate.ToString("yyMMdd")) <= int.Parse(DateTime.Today.ToString("yyMMdd")) + 3 && atask.Status != "Done")
                {
                    ListViewItem newitem = new ListViewItem();
                    newitem.Text = atask.Titel;

                    //ListViewItem.ListViewSubItem newsubitem = new ListViewItem.ListViewSubItem();
                    //newsubitem.Text = atask.TaskID.ToString();
                    //newitem.SubItems.Add(newsubitem);
                    //ListViewItem.ListViewSubItem newsubitem1 = new ListViewItem.ListViewSubItem();
                    //newsubitem1.Text = atask.Titel;
                    //newitem.SubItems.Add(newsubitem1);
                    //ListViewItem.ListViewSubItem newsubitem2 = new ListViewItem.ListViewSubItem();
                    //newsubitem2.Text = atask.Status.ToString();
                    //newitem.SubItems.Add(newsubitem2);
                    ListViewItem.ListViewSubItem newsubitem3 = new ListViewItem.ListViewSubItem();
                    newsubitem3.Text = atask.Sprintduedate.ToString("yy-MM-dd");
                    newitem.SubItems.Add(newsubitem3);
                    //ListViewItem.ListViewSubItem newsubitem4 = new ListViewItem.ListViewSubItem();
                    //newsubitem4.Text = atask.Sprinttitel.ToString();
                    //newitem.SubItems.Add(newsubitem4);
                    //ListViewItem.ListViewSubItem newsubitem5 = new ListViewItem.ListViewSubItem();
                    //newsubitem5.Text = atask.Teamname.ToString();
                    //newitem.SubItems.Add(newsubitem5);

                    //adding the taskid in order to use when opening the taskdetail form


                    newitem.Tag = atask.TaskID;

                    this.LvHotTasks.Items.Add(newitem);
                }

            }
            #endregion


        }


        private void ShowTotalList() //pretty much means that all items are shown => no filter
        {
            List<Tasks> thisuserstasks = this.Thisuserstasks;

            this.LvAllTasks.Items.Clear();

            foreach (Tasks atask in thisuserstasks)
            {
                ListViewItem newitem = new ListViewItem();
                newitem.Text = atask.Titel;

                //ListViewItem.ListViewSubItem newsubitem = new ListViewItem.ListViewSubItem();
                //newsubitem.Text = atask.TaskID.ToString();
                //newitem.SubItems.Add(newsubitem);
                //ListViewItem.ListViewSubItem newsubitem1 = new ListViewItem.ListViewSubItem();
                //newsubitem1.Text = atask.Titel;
                //newitem.SubItems.Add(newsubitem1);
                ListViewItem.ListViewSubItem newsubitem2 = new ListViewItem.ListViewSubItem();
                newsubitem2.Text = atask.Status.ToString();
                newitem.SubItems.Add(newsubitem2);
                ListViewItem.ListViewSubItem newsubitem3 = new ListViewItem.ListViewSubItem();
                newsubitem3.Text = atask.Sprintduedate.ToString("yy-MM-dd");
                newitem.SubItems.Add(newsubitem3);
                ListViewItem.ListViewSubItem newsubitem4 = new ListViewItem.ListViewSubItem();
                newsubitem4.Text = atask.Sprinttitel.ToString();
                newitem.SubItems.Add(newsubitem4);
                ListViewItem.ListViewSubItem newsubitem5 = new ListViewItem.ListViewSubItem();
                newsubitem5.Text = atask.Teamname.ToString();
                newitem.SubItems.Add(newsubitem5);

                //adding the taskid in order to use when opening the taskdetail form
                newitem.Tag = atask.TaskID;

                this.LvAllTasks.Items.Add(newitem);


            }
            this.LvAllTasks.Sort();
        }


        //this filters the list based on 1 string => this will REMOVE items with that status
        private void FilterFromList(string status1)
        {
            List<Tasks> thisuserstasks = this.Thisuserstasks;
            
            this.LvAllTasks.Items.Clear();

                foreach (Tasks atask in thisuserstasks)
                {
                    if (atask.Status != status1)
                    {
                        ListViewItem newitem = new ListViewItem();
                        newitem.Text = atask.Titel;

                        //ListViewItem.ListViewSubItem newsubitem = new ListViewItem.ListViewSubItem();
                        //newsubitem.Text = atask.TaskID.ToString();
                        //newitem.SubItems.Add(newsubitem);
                        //ListViewItem.ListViewSubItem newsubitem1 = new ListViewItem.ListViewSubItem();
                        //newsubitem1.Text = atask.Titel;
                        //newitem.SubItems.Add(newsubitem1);
                        ListViewItem.ListViewSubItem newsubitem2 = new ListViewItem.ListViewSubItem();
                        newsubitem2.Text = atask.Status.ToString();
                        newitem.SubItems.Add(newsubitem2);
                        ListViewItem.ListViewSubItem newsubitem3 = new ListViewItem.ListViewSubItem();
                        newsubitem3.Text = atask.Sprintduedate.ToString("yy-MM-dd");
                        newitem.SubItems.Add(newsubitem3);
                        ListViewItem.ListViewSubItem newsubitem4 = new ListViewItem.ListViewSubItem();
                        newsubitem4.Text = atask.Sprinttitel.ToString();
                        newitem.SubItems.Add(newsubitem4);
                        ListViewItem.ListViewSubItem newsubitem5 = new ListViewItem.ListViewSubItem();
                        newsubitem5.Text = atask.Teamname.ToString();
                        newitem.SubItems.Add(newsubitem5);

                        //adding the taskid in order to use when opening the taskdetail form
                        newitem.Tag = atask.TaskID;

                        this.LvAllTasks.Items.Add(newitem);
                    }


                }

            
        }


        //will load list WITHOUT the items with status1 or status2 string
        private void FilterFromList(string status1, string status2)
        {
            List<Tasks> thisuserstasks = this.Thisuserstasks;

            this.LvAllTasks.Items.Clear();

            foreach (Tasks atask in thisuserstasks)
            {
                if (atask.Status != status1 && atask.Status != status2)
                {
                    ListViewItem newitem = new ListViewItem();
                    newitem.Text = atask.Titel;

                    //ListViewItem.ListViewSubItem newsubitem = new ListViewItem.ListViewSubItem();
                    //newsubitem.Text = atask.TaskID.ToString();
                    //newitem.SubItems.Add(newsubitem);
                    //ListViewItem.ListViewSubItem newsubitem1 = new ListViewItem.ListViewSubItem();
                    //newsubitem1.Text = atask.Titel;
                    //newitem.SubItems.Add(newsubitem1);
                    ListViewItem.ListViewSubItem newsubitem2 = new ListViewItem.ListViewSubItem();
                    newsubitem2.Text = atask.Status.ToString();
                    newitem.SubItems.Add(newsubitem2);
                    ListViewItem.ListViewSubItem newsubitem3 = new ListViewItem.ListViewSubItem();
                    newsubitem3.Text = atask.Sprintduedate.ToString("yy-MM-dd");
                    newitem.SubItems.Add(newsubitem3);
                    ListViewItem.ListViewSubItem newsubitem4 = new ListViewItem.ListViewSubItem();
                    newsubitem4.Text = atask.Sprinttitel.ToString();
                    newitem.SubItems.Add(newsubitem4);
                    ListViewItem.ListViewSubItem newsubitem5 = new ListViewItem.ListViewSubItem();
                    newsubitem5.Text = atask.Teamname.ToString();
                    newitem.SubItems.Add(newsubitem5);

                    //adding the taskid in order to use when opening the taskdetail form
                    newitem.Tag = atask.TaskID;

                    this.LvAllTasks.Items.Add(newitem);
                }


            }
        }

        private void LvAllTasks_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (LvAllTasks.SelectedItems != null)
            {
                int taskid = (int)(LvAllTasks.FocusedItem.Tag);

                TasksDetailsForm tskdetailsform = new TasksDetailsForm(taskid);
                tskdetailsform.Show();

            }
        }

        private void CheckBoxShowOptions_ItemCheck(object sender, EventArgs e)
        {
            bool pendingbox = CheckBoxShowOptions.GetItemChecked(0);
            bool notstartedbox = CheckBoxShowOptions.GetItemChecked(1);
            bool donebox = CheckBoxShowOptions.GetItemChecked(2);

            if (pendingbox == true && notstartedbox == true && donebox == true)
            {
                ShowTotalList();
            }
            else if (pendingbox == true && notstartedbox == true && donebox == false)
            {
                FilterFromList("Done");
            }
            else if (pendingbox == true && notstartedbox == false && donebox == true)
            {
                FilterFromList("Not Started");
            }
            else if (pendingbox == false && notstartedbox == true && donebox == true)
            {
                FilterFromList("Started");
            }
            else if (pendingbox == true && notstartedbox == false && donebox == false)
            {
                FilterFromList("Not Started","Done");
            }
            else if (pendingbox == false && notstartedbox == true && donebox == false)
            {
                FilterFromList("Started", "Done");
            }
            else if (pendingbox == false && notstartedbox == false && donebox == true)
            {
                FilterFromList("Started", "Not Started");
            }
            else if (pendingbox == false && notstartedbox == false && donebox == false)
            {
                this.LvAllTasks.Items.Clear();
            }

            

            //give a color to the status with a conditional filter
            foreach (ListViewItem newitem in LvAllTasks.Items)
            {
                if (newitem.SubItems[1].Text == "Not Started")
                {
                    newitem.BackColor = System.Drawing.Color.Red;
                }
                else if (newitem.SubItems[1].Text == "Started")
                {
                    newitem.BackColor = System.Drawing.Color.Orange;
                }
                else if (newitem.SubItems[1].Text == "Done")
                {
                    newitem.BackColor = System.Drawing.Color.Green;
                }
            }
        }

        private void LvHotTasks_DoubleClick(object sender, EventArgs e)
        {
            if (LvHotTasks.SelectedItems != null)
            {
                TasksDetailsForm ntaskdetailsform = new TasksDetailsForm((int)LvHotTasks.FocusedItem.Tag);
                ntaskdetailsform.Show();
            }
        }

        private void openSelectedTaskToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (LvHotTasks.SelectedItems != null)
            {
                TasksDetailsForm ntaskdetailsform = new TasksDetailsForm((int)LvHotTasks.FocusedItem.Tag);
                ntaskdetailsform.Show();
            }
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string temp = tbFilePath.Text.ToString();

            DataLayer.dlsprintlist DLsprintlistfiller = new DataLayer.dlsprintlist();
            DataSet dtso = DLsprintlistfiller.SprintListExport();

            WorkbookEngine tempExport = new WorkbookEngine();

            tempExport.ExportToCsvFromDataSet(dtso);

        }
    }
}
